<template>
  <div class="requto">
    <!-- 再次报价 -->
    <el-descriptions title="询价信息" :labelStyle="{ width: '90px' }" :column="2">
      <el-descriptions-item label="型号">
      <DescriptionsTooltip :value="qutoData.product_partnumber">{{  qutoData.product_partnumber }}</DescriptionsTooltip>
      </el-descriptions-item>
      <el-descriptions-item label="品牌">
      <DescriptionsTooltip :value=" qutoData.product_brand">{{  qutoData.product_brand }}</DescriptionsTooltip>
      </el-descriptions-item>
      <el-descriptions-item label="数量">{{
        qutoData.quantity
      }}</el-descriptions-item>
      <el-descriptions-item label="批次">{{
        qutoData.product_dateCode
      }}</el-descriptions-item>
      <el-descriptions-item label="封装">
      <DescriptionsTooltip :value="qutoData.product_package ">{{ qutoData.product_package }}</DescriptionsTooltip>
      </el-descriptions-item>
      <el-descriptions-item label="接受价">
        <span style="color:#FF0000;">{{ Number.isInteger(qutoData.adoptPrice) ? qutoData.adoptPrice.toFixed(2) :
          qutoData.adoptPrice }} </span>
      </el-descriptions-item>
      <el-descriptions-item label="币种">{{
        qutoData.currency == "CNY" ? "人民币" : "美元"
      }}</el-descriptions-item>
      <el-descriptions-item label="交货日期">{{
        qutoData.deliveryDate
      }}</el-descriptions-item>
      <el-descriptions-item label="交货地">{{
        qutoData.deliveryPlace == "FArea01" ? "中国大陆" : "中国香港"
      }}</el-descriptions-item>
      <el-descriptions-item label="包装">{{
        qutoData.product_packing
      }}</el-descriptions-item>
      <el-descriptions-item label="发票类型">
      <DescriptionsTooltip :value=" qutoData.invoiceType ">{{ qutoData.invoiceType }}</DescriptionsTooltip>
      </el-descriptions-item>
      <el-descriptions-item label="剩余报价时间">
        <span style="color: #e02020">{{
          (0 >= qutoData.quoteRemainMin * 1
            ? "已结束"
            : qutoData.quoteRemainMin) | manageTime
        }}</span>
      </el-descriptions-item>
    </el-descriptions>

    <label class="suptitle">历史报价</label>
    <div>
      <el-table :data="tableData" size="mini" :loading="tableLoading" border :cell-style="{ 'text-align': 'center' }"
        :header-cell-style="{ 'text-align': 'center' }">
        <el-table-column prop="product" label="报价时间" width="100">
          <template v-slot="{ row }">
            {{ row.product.createDate }}
          </template>
        </el-table-column>
        <el-table-column prop="inPrice" label="报价" width="60">
          <template v-slot="{ row }">
            <span style="color:#00AB2B">{{ Number.isInteger(row.inPrice.unitPrice) ? row.inPrice.unitPrice.toFixed(2) :
              row.inPrice.unitPrice }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="inPrice" label="币种" width="60">
          <template v-slot="{ row }">
            {{ row.inPrice.currency == 1 ? "人民币" : "美元" }}
          </template>
        </el-table-column>
        <el-table-column prop="quantity" label="数量" width="50"></el-table-column>
        <el-table-column prop="deliveryDate" label="交货日期" width="80">
          <template v-slot="{ row }">
            {{ row.deliveryDate && row.deliveryDate.split(" ")[0] }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="termDate" label="报价有效期" width="100">
          <template v-slot="{ row }">
            {{ row.termDate && row.termDate.split(" ")[0] }}
          </template> 
        </el-table-column>-->

        <el-table-column prop="product" label="批次" show-overflow-tooltip width="50">
          <template v-slot="{ row }">
            {{ row.product.dateCode }}
          </template>
        </el-table-column>
        <el-table-column prop="deliveryPlace" label="交货地">
          <template v-slot="{ row }">
            {{ row.deliveryPlace == "FArea01" ? "中国大陆" : "中国香港" }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <label class="suptitle">本次报价</label>
    <el-form :model="ruleForm" :rules="rules" ref="form" label-width="100" label-position="left">
      <el-form-item label="" v-if="isActurl" v-show="isShowCheckbox" class="isAccept">
        <el-checkbox v-model="isAccept" @change="handleAccept" name="quto">一键同意</el-checkbox>
      </el-form-item>
      <el-form-item label="币种：" prop="currency">
        <!-- <template v-if="isAccept">
          {{ruleForm.currency=="CNY"?"人民币":"美元"}}
        </template> -->
        <!-- <template v-else> -->
        <el-select v-model="ruleForm.currency" @change="changeOptions($event)" class="input">
          <el-option label="人民币" value="CNY"></el-option>
          <el-option label="美元" value="USD"></el-option>
        </el-select>
        <!-- </template> -->
      </el-form-item>
      <el-form-item label="单价：" prop="unitPrice">
        <!-- <template v-if="isAccept">
          {{ruleForm.unitPrice}}
        </template> -->
        <!-- <template v-else> -->
        <!-- <el-input-number :controls="false" :max="999999999" v-model="ruleForm.unitPrice" :formatter="formatValue" :parser="parseValue"
          style="margin-left: 27px;"></el-input-number> -->
        <el-input v-model="ruleForm.unitPrice" @blur="handleBlur" @input="handleInput" class="input"></el-input>
        <!-- </template> -->
      </el-form-item>
      <el-form-item label="报价数量：" prop="quantity">
        <!-- <template v-if="isAccept">
          {{ruleForm.quantity}}
        </template>
        <template v-else> -->
        <el-input-number :controls="false" v-inpNum.noquto :max="999999999" v-model="ruleForm.quantity"></el-input-number>
        <!-- </template> -->
      </el-form-item>
      <el-form-item label="交货日期：" prop="deliveryDate">
        <!-- <template v-if="isAccept">
          {{ruleForm.quantity}}
        </template>
        <template v-else> -->
        <el-date-picker v-model="ruleForm.deliveryDate" :picker-options="pickerOption" type="date"
          value-format="yyyy-MM-dd" placeholder="选择日期">
        </el-date-picker>
        <!-- </template> -->
      </el-form-item>
      <el-form-item label="批次：" prop="dateCode" class="dateCode">
        <!-- <template v-if="isAccept">
          {{ruleForm.dateCode}}
        </template>
        <template v-else> -->
        <el-input v-model="ruleForm.dateCode" maxlength="30" type="text" show-word-limit class="dateCodeinput"></el-input>
        <!-- </template> -->
      </el-form-item>
      <el-form-item label="交货地：" prop="deliveryPlace">
        <!-- <template v-if="isAccept">
          {{ruleForm.deliveryPlace=="FArea01"?"国内":"香港"}}
        </template>
        <template v-else> -->
        <el-select v-model="ruleForm.deliveryPlace" class="place">
          <el-option label="中国大陆" value="FArea01"></el-option>
          <el-option label="中国香港" value="FArea02"></el-option>
        </el-select>
        <!-- </template> -->
      </el-form-item>

      <el-form-item label="报价有效期：" prop="termDate" v-show="false">
        <!-- <template v-if="isAccept">
          {{ruleForm.quantity}}
        </template>
        <template v-else> -->
        <el-date-picker v-model="ruleForm.termDate" :picker-options="pickerOption" type="date" value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
        <!-- </template> -->
      </el-form-item>
      <el-form-item label="报价公司：" prop="supplierID">
        <el-select v-model="ruleForm.supplierID" @change="handlesupplierChange">
          <el-option v-for="item in suppliers" :label="item.supplierName" :value="item.supplierID" :key="item.supplierID">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="foot" v-loading="btnLoading">
      <el-button type="primary" size="small" @click="handleConfirmQuto">
        确认报价</el-button>
      <!-- <el-button plain size="small" @click="$emit('close')">取消</el-button> -->
    </div>
  </div>
</template>

<script>
import { TaskQuote, getSuppliers } from "@/api/scsapi/index";
import DescriptionsTooltip from "@/components/DescriptionsTooltip/index.vue";
import { nextDate } from "@/utils";
export default {
  name: "quto",
  components: {
    DescriptionsTooltip
  },
  props: {
    isShowCheckbox: {
      default: true,
      type: Boolean,
    },
    isActurl: {
      default: true,
      type: Boolean,
    },
    qutoData: {
      default: () => {
        return {};
      },
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      tableData: [],
      // 是否同意接受价
      isAccept: false,
      tableLoading: false,
      btnLoading: false,
      ruleForm: {
        unitPrice: undefined,
        currency: undefined,
        quantity: undefined,
        deliveryDate: undefined,
        dateCode: undefined,
        deliveryPlace: undefined,
        supplierID: undefined,
        supplierName: undefined,
        summary: "",
        termDate: nextDate(),
      },
      pickerOption: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },

      rules: {
        unitPrice: [{ required: true, message: "请输入报价", trigger: "blur" }],
        currency: [
          { required: true, message: "请选择币种", trigger: "change" },
        ],
        quantity: [{ required: true, message: "请输入数量", trigger: "blur" }],
        deliveryDate: [
          { required: true, message: "请选择交货日期", trigger: "change" },
        ],
        dateCode: [
          // { required: true, message: '请输入批次', trigger: 'blur' },
          { min: 0, max: 50, message: "长度0-50", trigger: "blur" },
        ],
        deliveryPlace: [
          { required: true, message: "请选择交货地", trigger: "change" },
        ],
        supplierID: [
          { required: true, message: "请选择报价公司", trigger: "change" },
        ],
        termDate: [
          { required: true, message: "请选择报价有效期", trigger: "change" },
        ],
      },
      suppliers: [],
    };
  },
  created() {
    this.tableData = this.qutoData.historyQuotes;
    getSuppliers().then((res) => {
      if (res.status == 200) {
        this.suppliers = res.data;
        console.log('tag', this.suppliers)
      }
    });
  },
  mounted() {
    console.log('tag', this.qutoData)
    // this.ruleForm.unitPrice = this.qutoData.adoptPrice ? this.qutoData.adoptPrice : 0;
    this.ruleForm.currency = this.qutoData.currency;
    // this.ruleForm.quantity = this.qutoData.quantity;
    this.ruleForm.deliveryDate = this.qutoData.deliveryDate;
    this.ruleForm.dateCode = this.qutoData.dateCode;
    this.ruleForm.deliveryPlace = this.qutoData.deliveryPlace;
    this.ruleForm.supplierID = this.qutoData.quote && this.qutoData.quote.supplier && this.qutoData.quote.supplier.name
  },
  methods: {
    handleInput() {
      const value = this.ruleForm.unitPrice.trim();
      const reg = /^\d*$/;
      if (!reg.test(value)) {
        this.ruleForm.unitPrice = value.replace(/[^\d.]/g, '');
      }
    },
    handleBlur() {
      const value = this.ruleForm.unitPrice.trim();
      if (/^\d+$/.test(value)) {
        this.ruleForm.unitPrice = value + '.00';
      }
    },
    // 选中人民币就选中国内
    changeOptions(parsms) {
      console.log('tag', parsms)
      if (parsms == 'CNY') {
        this.ruleForm.deliveryPlace = 'FArea01'
      } else {
        this.ruleForm.deliveryPlace = 'FArea02'
      }
    },
    handlesupplierChange(v) {
      let sup = this.suppliers.find((x) => x.supplierID == v);
      if (sup) {
        this.ruleForm.supplierName = sup.supplierName;
        this.ruleForm.supplierID = sup.supplierID
      } else {
        this.ruleForm.supplierName = undefined;
        this.ruleForm.supplierID = undefined;
      }
    },
    // 历史报价数据
    getHistoryTableData() {
      this.tableLoading = true;
      setTimeout(() => {
        this.tableData = [];
        this.tableLoading = false;
      }, 1000);
    },
    // 确认报价
    handleConfirmQuto() {
      this.btnLoading = true;
      this.$refs.form.validate((valid) => {
        if (valid) {
          TaskQuote({
            inquiryID: this.qutoData.inquiryID,
            inquiryItemID: this.qutoData.product_id,
            unitPrice: this.ruleForm.unitPrice,
            currency: this.ruleForm.currency,
            quantity: this.ruleForm.quantity,
            deliveryDate: this.ruleForm.deliveryDate,
            dateCode: this.ruleForm.dateCode,
            deliveryPlace: this.ruleForm.deliveryPlace,
            supplierID: this.qutoData.quote.supplierID,
            supplierName: this.ruleForm.supplierName,
            summary: undefined,
            termDate: this.ruleForm.termDate || "", // 报价有效期
          }).then((res) => {
            if (res.status == 200) {
              this.$emit("close");
              this.$message.success("报价成功");
            }
            this.btnLoading = false;
          });
        } else {
          this.btnLoading = false;
        }
      });
    },
    handleAccept() {
      if (this.isAccept) {
        this.ruleForm.unitPrice = this.qutoData.adoptPrice
          ? this.qutoData.adoptPrice
          : 0;
        this.ruleForm.currency = this.qutoData.currency;
        this.ruleForm.quantity = this.qutoData.quantity;
        this.ruleForm.deliveryDate = this.qutoData.deliveryDate;
        this.ruleForm.dateCode = this.qutoData.product_dateCode;
        this.ruleForm.deliveryPlace = this.qutoData.deliveryPlace;
      } else {
        this.$refs["form"].resetFields();
      }
    },
    // 无法报价
  },
  filters: {
    manageTime(v) {
      if (!v || isNaN(v)) {
        return v;
      } else {
        // let hour = parseInt(v / 3600);
        // let min = parseInt((v - hour * 60 * 60) / 60);
        // let sec = parseInt((v % 3600) - min * 60);
        // return `${hour < 10 ? "0" + hour : hour}:${min < 10 ? "0" + min : min
        //   }:${sec < 10 ? "0" + sec : sec}`;
        let days = parseInt(v / (60 * 60 * 24));
        let hour = parseInt((v % 86400) / 3600);
        let min = parseInt((v % 3600) / 60);
        return `${days}天${hour}时${min}分`
      }
    },
  },
  directives: {
    inpNum: {
      inserted: function (el, binding) {
        el.oninput = function (v) {
          setTimeout(() => {
            let value = v.target.value;
            if (binding.modifiers.noquto) {
              value = value.replace(/[^\d]+/g, "");
            } else {
              value = value.replace(/[^\d^\.]+/g, "");
            }
            v.target.value = value;
          });
        };
      },
    },
    decimalize: {
      inserted(el) {
        el.addEventListener('input', function (event) {
          setTimeout(() => {
            const value = parseFloat(event.target.value);
            if (Number.isInteger(value)) {
              event.target.value = value + '.00';
            } else {
              event.target.value = value
            }
          })
        });
      }
    }
    // remainedTime: {
    //   inserted: function (el, binding) {
    //     let interval = null;
    //     let html = el.innerHTML;
    //     function timex() {
    //       html = el.innerHTML;
    //       if (!html.includes("已结束")) {
    //         let v =
    //           html.split(":")[0] * 3600 +
    //           html.split(":")[1] * 60 +
    //           html.split(":")[2] * 1;
    //         v = v - 1;
    //         if (v <= 0) {
    //           el.innerHTML = "已结束";
    //           binding.value.quoteRemainMin = 0;
    //           interval || clearInterval(interval);
    //         } else {
    //           binding.value.quoteRemainMin = v;
    //           let hour = parseInt(v / 3600);
    //           let min = parseInt((v - hour * 60 * 60) / 60);
    //           let sec = parseInt((v % 3600) - min * 60);
    //           el.innerHTML = `${hour < 10 ? "0" + hour : hour}:${min < 10 ? "0" + min : min
    //             }:${sec < 10 ? "0" + sec : sec}`;
    //         }
    //       }
    //     }
    //     if (!html.includes("已结束")) {
    //       timex();
    //       interval = setInterval(() => {
    //         timex();
    //       }, 1000);
    //       if (interval) {
    //         el.setAttribute("data", interval);
    //       }
    //     }
    //   },
    //   unbind: function (el) {
    //     let data = el.getAttribute("data");
    //     if (data) {
    //       clearInterval(data);
    //     }
    //   },
    // },
  },
};
</script>
<style scoped lang="scss" src="@/assets/styles/public.scss"></style>
<style scoped lang="scss">
::v-deep .el-drawer {
  background: red !important;

  ::v-deep .el-drawer__title .el-drawer__header {
    margin-bottom: 10px !important;
  }
}

::v-deep .el-descriptions__header {
  // color: #008fff;
  // margin: 0 20px 20px 20px;
  color: #1A2035;
  font-weight: 500;
  font-size: 12px;
  margin-bottom: 15px;

  .el-descriptions__title {
    font-size: 14px;
  }
}

::v-deep .el-descriptions__body {
  // margin: 0 20px;
  border: 1px solid #ccc;
  padding: 12px 20px;
  border-radius: 4px;

  .el-descriptions-item__container {
    justify-content: space-between !important;

    .el-descriptions-item__content {
      margin-right: 40px;
    }
  }

  .el-descriptions-item__label {
    color: #999;
    font-size: 12px;
  }

  .el-descriptions-item__content {
    color: #1A2035;
    font-size: 12px;
    font-weight: 400;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

}

.suptitle {
  color: #1A2035;
  display: block;
  font-weight: 600;
  font-size: 14px;
  padding: 15px 0;
}

.el-table {
  border-radius: 4px;
}

.el-form {
  // margin: 0 20px;
  border: 1px solid #ccc;
  padding: 12px 20px;
  border-radius: 4px;

  // ::v-deep .el-input{
  //   width: 100%;
  // }
  // ::v-deep .el-select {
  //   ::v-deep.el-input{
  //     width: 100%;
  //   }
  //   // .el-input--suffix{
  //   //   width: 100%;
  //   // }
  // }
  ::v-deep.el-form-item__label {
    font-size: 12px;
    color: #999;
  }

  .el-checkbox {
    width: 110px;
    height: 20px;
    line-height: 20px;
    background: #2348E8;
    border-radius: 2px;
    color: #fff;
    text-align: center;

    ::v-deep.el-checkbox__label {
      font-size: 12px !important;
      color: #fff !important;
    }

    ::v-deep.el-checkbox__input {
      .el-checkbox__inner {
        background: #2348E8;
        border-color: #fff;
      }

      .el-checkbox__inner:hover {
        background: #2348E8 !important;
        border-color: #fff !important;
      }
    }

    ::v-deep.el-checkbox__input.is-checked {
      .el-checkbox__inner {
        background: #2348E8 !important;
        border-color: #fff !important;
      }
    }
  }

  .input {
    margin-left: 24px;
  }

  .el-select--small {
    width: 84%;
  }

  .el-input--small {
    width: 84%;
  }

  .el-input-number--small {
    width: 84%;
  }

  ::v-deep .el-form-item__label-wrap {
    margin-left: 0px !important;
  }

  .isAccept {
    ::v-deep .el-form-item__content {
      margin-left: 0px !important;
    }
  }

  .dateCode {
    ::v-deep .el-form-item__label {
      margin-left: 8px !important;
    }

    .dateCodeinput {
      margin-left: 25px;
    }
  }

  .place {
    margin-left: 13px;
  }

  ::v-deep .el-form-item__error {
    // top: 10px;
    left: 68px;
  }

  // .el-form-item {
  //   margin-bottom: 10px;
  // }

  ::v-deep .el-input__inner {
    text-align: left;
    color: #1A2035;
  }
}

.foot {
  // margin: 10px 20px;
  display: flex;
  justify-content: center;
  position: absolute;
  left: 45%;
  bottom: 1%;
}</style >
 